package com.yxx.service.Impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxx.entity.ArticleTag;
import com.yxx.entity.Tag;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.Result.ResponseResult;
import com.yxx.entityModel.vo.query.ArticleConditionQuery;
import com.yxx.entityModel.vo.query.TagQuery;
import com.yxx.entityModel.vo.request.TagRequest;
import com.yxx.entityModel.vo.response.*;
import com.yxx.mapper.ArticleMapper;
import com.yxx.mapper.ArticleTagMapper;
import com.yxx.mapper.TagMapper;
import com.yxx.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author yuxiangxun
 * @date 2025/2/2
 * @apiNote
 */
@Service
@Slf4j
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Resource
    private TagMapper tagMapper;

    @Resource
    private ArticleTagMapper articleTagMapper;

    @Resource
    private ArticleMapper articleMapper;

    /**
     * 查看后台标签列表
     * @param tagQuery
     * @return
     */
    @Override
    public PageResult<TagBackResponse> tagBackListVO(TagQuery tagQuery) {
        // 查询后台标签数量  通过模糊查询标签关键字获取后台标签数量
        Long count = tagMapper.selectCount(new LambdaQueryWrapper<Tag>()
                .like(StringUtils.hasText(tagQuery.getKeyword()), Tag::getTagName, tagQuery.getKeyword()));
        // 如果后台标签数量为0，则new一个分页结果对象并返回
        if(count == 0){
            return new PageResult<>();
        }
        // 如果后台标签数量不为0，则分页查询后台标签列表
        List<TagBackResponse> tagBackResponsesList = tagMapper.selectTagBackList(tagQuery);
        return new PageResult<>(tagBackResponsesList,count);
    }

    /**
     * 添加标签
     * @param tagRequest
     */
    @Override
    public void addTag(TagRequest tagRequest) {
        // 标签是否存在 通过标签请求信息查询标签信息（值只有标签id的对象）
        Tag existTag = tagMapper.selectOne(new LambdaQueryWrapper<Tag>().select(Tag::getId).eq(Tag::getTagName, tagRequest.getTagName()));
        // 断言，标签存在抛出异常“xxx表标签已存在”
        Assert.isNull(existTag,tagRequest.getTagName()+"标签已经存在");
        // 标签不存在则添加新标签  创建新标签对象(值只有标签名称)
        Tag newTag = Tag.builder()
                .tagName(tagRequest.getTagName())
                .build();
        baseMapper.insert(newTag);
    }

    /**
     * 删除标签
     * @param tagIdList
     */
    @Override
    public void deleteTag(List<Integer> tagIdList) {
        // 标签下是否有文章  根据标签id查询文章标签数量
        Long count = articleTagMapper.selectCount(new LambdaQueryWrapper<ArticleTag>().in(ArticleTag::getTagId, tagIdList));
        // 断言，有文章则抛出异常“xxx表标签下有文章，删除失败”
        Assert.isFalse(count > 0, "删除失败，标签下存在文章");
        // 没有文章则批量删除标签
        tagMapper.deleteBatchIds(tagIdList);
    }

    /**
     * 修改标签
     * @param tagRequest
     */
    @Override
    public void updateTag(TagRequest tagRequest) {
        // 查询标签是否存在，只需要查询到该标签id即可
        Tag existTag = tagMapper.selectOne(new LambdaQueryWrapper<Tag>().select(Tag::getId).eq(Tag::getTagName, tagRequest.getTagName()));
        // 断言 这两两个条件都为假才能通过断言
        Assert.isFalse(Objects.nonNull(existTag) && !existTag.getId().equals(tagRequest.getId()), tagRequest.getTagName()+"标签已经存在");
        // 查询到标签id对应的标签对象不为空且查询出来的标签id与请求中的标签id一致 修改标签
        Tag newTag = Tag.builder()
                .tagName(tagRequest.getTagName())
                .id(tagRequest.getId())
                .build();
        baseMapper.updateById(newTag);
    }

    /**
     * 查看标签选项
     * @return
     */
    @Override
    public List<TagOptionResponse> tagListOption() {
        return tagMapper.selectTagOptionList();
    }

    /**
     * 查看标签列表
     * @return
     */
    @Override
    public List<TagResponse> tagListVO() {
        return tagMapper.selectTagListVO();
    }

    /**
     * 查看标签下的文章列表
     * @param articleConditionQuery
     * @return
     */
    @Override
    public ArticleConditionListResponse articleTagList(ArticleConditionQuery articleConditionQuery) {
        List<ArticleConditionResponse> articleConditionVOList = articleMapper.selectArticleListByCondition(articleConditionQuery);
        String tagName = tagMapper.selectOne(new LambdaQueryWrapper<Tag>().select(Tag::getTagName).eq(Tag::getId, articleConditionQuery.getTagId())).getTagName();
        return ArticleConditionListResponse.builder()
                .articleConditionVOList(articleConditionVOList)
                .name(tagName)
                .build();
    }

    /**
     * 批量添加标签
     * @param newTagList
     */
    @Override
    public void saveBatch(List<Tag> newTagList) {
        this.saveBatch(newTagList);
    }
}
